<?php

namespace app\models\sdiot;

use yii\base\Exception;

/**
 * 短信相关
 * Trait ApiSmsTrait
 * @package app\models\sdiot
 */
trait ApiSmsTrait
{
    /**
     * 发送下行短信
     * @param array $dests 短信号码
     * @param string $content 短信内容
     * @param integer $msgFmt 编码 0：ascii 8：USC2 默认为USC2
     * @return boolean
     * @throws Exception
     */
    public function smsSendUpSMS($dests, $content, $msgFmt = 8)
    {
        $data = [
            'spCode' => $this->smsSpCode,
            'spId' => $this->smsSpId,
            'serviceId' => $this->smsServiceId,
            'loginName' => $this->smsLoginName,
            'password' => $this->smsPassword,
            'msgFmt' => $msgFmt,
            'content' => $content,
            'dests' => $dests,
        ];
        $json = $this->post2('/sdiot/smsopen/sendUpSMS', $data);
        if ($json['responseHeader']['resultCode'] != '000000') {
            throw new Exception($json['resultMessage']);
        }
        return true;
    }

    /**
     * 读取上行短信接口
     * @return boolean
     * @throws Exception
     */
    public function smsReadDownSMS()
    {
        $data = [
            'spCode' => $this->smsSpCode,
            'spId' => $this->smsSpId,
            'serviceId' => $this->smsServiceId,
            'loginName' => $this->smsLoginName,
            'password' => $this->smsPassword,
        ];
        $json = $this->post1('/sdiot/smsopen/readDownSMS', $data);
        if ($json['responseHeader']['resultCode'] != '000000') {
            throw new Exception($json['resultMessage']);
        }
        return true;
    }

    /**
     * 分页查询短信列表接口
     * @param string $targetCode 对端号码
     * @param integer $smsType 短信类型： 0 下行 1 上行
     * @param integer $sendStatus 短信发送状态 1 发送成功 2 发送失败 3 上行未读取 4 上行已读取
     * @param integer $startTime 发送开始时间
     * @param integer $endTime 发送结束时间
     * @param boolean $flag 是否分页
     * @param integer $currentPage 当前页
     * @param integer $pageSize 页大小
     * @return array
     * {
     *     totalCount,
     *     detailsList: [
     *         {
     *             content, spCode, createTime, serviceId, smsType, sendTime, sendStatus, targetCode, spId, loginName
     *         },
     *         ...
     *     ]
     * }
     * @throws Exception
     */
    public function smsQueryOpenSMSList($targetCode = null, $smsType = null, $sendStatus = null, $startTime = null, $endTime = null, $flag = null, $currentPage = null, $pageSize = null)
    {
        $data = [
            'spCode' => $this->smsSpCode,
            'spId' => $this->smsSpId,
            'serviceId' => $this->smsServiceId,
            'loginName' => $this->smsLoginName,
        ];
        if (!empty($targetCode)) {
            $data['targetCode'] = $targetCode;
        }
        if ($smsType !== null) {
            $data['smsType'] = $smsType;
        }
        if ($smsType !== null) {
            $data['sendStatus'] = $sendStatus;
        }
        if ($startTime !== null) {
            $data['startTime'] = date('Y-m-d H:i:s', $startTime);
        }
        if ($endTime !== null) {
            $data['endTime'] = date('Y-m-d H:i:s', $endTime);
        }
        if ($flag !== null) {
            $data['flag'] = $flag;
        }
        if ($currentPage !== null) {
            $data['currentPage'] = $currentPage;
        }
        if ($pageSize !== null) {
            $data['pageSize'] = $pageSize;
        } else {
            $data['pageSize'] = 10;
        }
        $json = $this->post2('/sdiot/smsopen/queryOpenSMSList', $data);
        if ($json['responseHeader']['resultCode'] != '000000') {
            throw new Exception($json['resultMessage']);
        }
        return $json;
    }
}
